import * as config from '../config';
import * as storage from '../utils/storage';

interface i_params{
	url: string,
	data: any,
	method?: string
    type?: any
}
const requestFun = ({url, data, method, type}) => {
    const tokenName = type === 'zs' ? 'web-token' : config.AUTH_HEADER_NAME
    return new Promise((resolve, reject) => {
        var CURRENT_HOST:string = process.env.NODE_ENV === 'development' ? config.API_HOST_DEV : config.API_HOST
        uni.request({
            url: `${CURRENT_HOST}/admin${url}`,
            header: {
                [tokenName]: storage.token(),
                ['wechat-mp-request']: true
            },
            data: data || {},
            dataType: 'json',
            method: method || 'post',
            success: res => {
                if (res.statusCode === 401) {
                    storage.logout();
                    const pages = getCurrentPages();

                    if (pages.length === 0) {
                        const { route, options } = pages[pages.length - 1];
                        const query = [];
                        for (let key in options) {
                            query.push(`${key}=${options[key]}`);
                        }
                        const redirect = encodeURIComponent(`/${route}${query.length ? '?' : ''}${query.join('&')}`);

                        return uni.redirectTo({
                            url: `/pages/login/login?redirect=${redirect}`
                        });
                    } else {
                        return uni.redirectTo({
                            url: '/pages/login/login'
                        });
                    }
                }

                switch (res.statusCode) {
                    case 400:
                        res.data = { message: '错误请求' };
                        break;

                    case 403:
                        res.data = { message: '权限不足，拒绝访问' };
                        break;
                    case 404:
                        res.data = { message: '请求错误，未找到该资源' };
                        break;
                    case 405:
                        res.data = { message: '请求方法未允许' };
                        break;
                    case 408:
                        res.data = { message: '请求超时' };
                        break;
                    case 500:
                        res.data = { message: '服务器端出错' };
                        break;
                    case 501:
                        res.data = { message: '网络未实现' };
                        break;
                    case 502:
                        res.data = { message: '网络错误' };
                        break;
                    case 503:
                        res.data = { message: '服务不可用' };
                        break;
                    case 504:
                        res.data = { message: '网络超时' };
                        break;
                    case 505:
                        res.data = { message: 'http版本不支持该请求' };
                        break;
                }

                if ((res.data.code === 200 && res.statusCode === 200) || (res.data.code === 0 && res.statusCode === 200)) {
                    resolve(res.data);
                } else {
                    reject(res.data);
                }
            },
            fail: res => {
                reject({ message: res.errMsg });
            }
        });
    });
}
export function ask({url,data,method}: i_params) {
    return requestFun({url,data,method, type: 'zs'})
}
export function request({ url, data, method }: i_params) {
    return requestFun({url, data, method})

}




